package com.chinasofti.thread02;

public class Demo02 {
    //TIMED_WAITING
    public static void main(String[] args) throws InterruptedException {

        Object obj = new Object();

        //定义一个内部线程
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("2.执行thread.start后，线程的状态"+Thread.currentThread().getState());
                synchronized (obj){
                    try {
                        Thread.sleep(100);

                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("4.执行thread.被notify唤醒后，线程的状态"+Thread.currentThread().getState());
            }
        });
        //获取start 之前的状态
        System.out.println("1.通过new初始化一个线程，但是没有调用start之前，线程的状态"+thread.getState());
        //启动线程
        thread.start();
        //休眠50
        Thread.sleep(150);
        //因为thread1需要休眠100毫秒
        System.out.println("3.执行sleep50，线程状态"+thread.getState());
        //声明一个唤醒线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (obj){
                    obj.notify();
                }
            }
        }).start();
        //
        Thread.sleep(100);
        System.out.println("5.线程执行完毕，线程状态"+thread.getState() +"\n");
    }
}